<!DOCTYPE html>
<html>
<script src="../../resources/js-test.js"></script>
<body>
<script id="myWorker" type="text/worker">
self.onmessage = function(e) {
  var aCanvas = new OffscreenCanvas(50, 50);
  self.postMessage({version:'first', width:aCanvas.width, height:aCanvas.height});

  aCanvas.width = 100;
  aCanvas.height = 100;
  self.postMessage({version:'second', width:aCanvas.width, height:aCanvas.height});
};
</script>

<script>
jsTestIsAsync = true;
description("Tests that the OffscreenCanvas can be constructed on a worker thread.");

var width;
var height;

function makeWorker(script) {
  var blob = new Blob([script]);
  return new Worker(URL.createObjectURL(blob));
}

function handleMessageFromWorker(msg) {
  width = msg.data.width;
  height = msg.data.height;
  switch (msg.data.version) {
    case 'first':
      shouldBe('width', '50');
      shouldBe('height', '50');
      break;
    case 'second':
      shouldBe('width', '100');
      shouldBe('height', '100');
      finishJSTest();
      break;
    default:
      testFailed("Unexpected failure");
      break;
  }
}

var worker = makeWorker(document.getElementById('myWorker').textContent);
worker.addEventListener('message', handleMessageFromWorker);
worker.postMessage("");
</script>

</body>
</html>
